<template>
  <div style="width:100%;height:100%;" v-loading="loading">
    <el-dialog title="添加客服常用语" :close-on-click-modal="false" :visible.sync="dialogVisible" width="30%" @close="closeHandler">
      <el-form :model="commonWords" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题名称" prop="titleName">
          <el-input v-model="commonWords.titleName"></el-input>
        </el-form-item>
        <el-form-item label="标题描述" prop="titleDesc">
          <el-input v-model="commonWords.titleDesc" ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="type" >
          <el-select v-model="commonWords.status" placeholder="设置状态" >
            <el-option  label="正常" :value="1" key="1"></el-option>
            <el-option  label="无效" :value="2" key="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="closeHandler()">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {commonWordsSaveUrl} from '@/utils/common'
export default {
  props: {
    commonWordsDialog: {
      type: Boolean,
      default: false
    },
    popCommonWords: {
      type: Object,
      default: null
    }
  },
  watch: {
    commonWordsDialog() {
    //   console.log("---------------")
      this.dialogVisible = this.commonWordsDialog;
    },
    popCommonWords() {
      this.commonWords = this.popCommonWords; 
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      commonWords: {
        status: 1,
      },
      rules: {
        mobile: { required: true, message: "请输入员工电话", trigger: "blur" },
        chineseName: { required: true, message: "请输入姓名", trigger: "blur" },
      }
    }
  },
  mounted() {
    // this.loadShops();
    // this.filterPosition();

  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveCommonWords();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    saveCommonWords() {
      this.axios
        .post(commonWordsSaveUrl, this.commonWords)
        .then(res => {
          if (res.data.status == "SUCCESS") {
            this.loading = false;
            this.$emit("close");
            this.$message.success(res.data.result);
          } else {
            this.loading = false;
            this.$message.error(res.data.result);
          }
        })
        .catch(response => {
          this.loading = false;
          console.log("response - " + response);
          this.$message.error("数据保存异常失败");
        });
    },
    closeHandler: function () {
      this.dialogVisible = false;
      this.commonWords = {status:1};
      this.$emit("close");
    }
  }
};
</script>

<style lang="sass" scoped>
</style>
